package me.zhengjie.modules.quartz.task.onlyOnce;

import cn.hutool.core.util.NumberUtil;
import com.google.common.collect.Lists;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import com.qiaofang.applet.service.other.service.CorporationService;
import com.qiaofang.applet.service.property.domain.FirsthandEstate;
import com.qiaofang.applet.service.property.service.FirsthandEstateService;
import com.qiaofang.applet.service.property.service.PropertyService;
import com.qiaofang.common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: youqing.yang
 * @Create: 2021/01/12 11:27
 * @Description:
 **/
@Slf4j
@Component
public class FirstEstateTotalPriceTask {

    @Autowired
    private CorporationService corporationService;

    @Autowired
    private FirsthandEstateService firsthandEstateService;

    @Autowired
    private MongoTemplate mongoTemplate;

    public void run() {
        log.info("FirstEstateTotalPriceTask run start");
        Corporation corporation = new Corporation();
        corporation.setDeleted(Boolean.FALSE);
        Iterable<Corporation> list = corporationService.findAll(Example.of(corporation));
        list.forEach(item -> {
            String corporationCode = item.getCorporationCode();
            if (StringUtil.isEmpty(item.getPullDomain())) {
                log.error("集团:{} 的同步数据域名未配置", corporationCode);
                return;
            }
            log.info("开始处理集团:{}的新房总价数据，开始时间:{}", item.getCorporationCode(), new Date());
            firstEstateTotalPriceHandle(item.getCorporationCode());
            log.info("完成处理集团:{}的新房总价数据，结束时间:{}", item.getCorporationCode(), new Date());
        });
        log.info("FirstEstateTotalPriceTask run end");
    }

    private void firstEstateTotalPriceHandle(String corporationCode){
        Query query = new Query();
        query.addCriteria(Criteria.where("corporationCode").is(corporationCode));
        query.addCriteria(Criteria.where("averagePrice").ne(null));
        query.addCriteria(Criteria.where("buildingAreaRange").ne(null));
        //查询出来的结果是存在户型面积的
        List<FirsthandEstate> firsthandEstateList = mongoTemplate.find(query, FirsthandEstate.class);
        List<FirsthandEstate> modifyList = Optional.ofNullable(firsthandEstateList).orElseGet(Lists::newArrayList)
                .stream().map(firsthandEstate -> {
                    BigDecimal averagePrice = firsthandEstate.getAveragePrice();
                    //单位：万
                    BigDecimal price = NumberUtil.div(averagePrice, 10000, 2);
                    //如果最大的户型面积不存在，那么最大户型面积和最小户型面积相等
                    if (firsthandEstate.getMaxBuildingArea() == null) {
                        BigDecimal totalPrice = NumberUtil.mul(price, firsthandEstate.getMinBuildingArea());
                        firsthandEstate.setMinTotalPrice(totalPrice);
                        firsthandEstate.setMaxTotalPrice(totalPrice);
                    }
                    if (firsthandEstate.getMinBuildingArea() != null) {
                        firsthandEstate.setMinTotalPrice(NumberUtil.mul(price, firsthandEstate.getMinBuildingArea()));
                    }
                    if (firsthandEstate.getMaxBuildingArea() != null) {
                        firsthandEstate.setMaxTotalPrice(NumberUtil.mul(price, firsthandEstate.getMaxBuildingArea()));
                    }
                    return firsthandEstate;
                }).collect(Collectors.toList());
        firsthandEstateService.saveAll(modifyList);
    }


    public static void main(String[] args) {

        BigDecimal price = NumberUtil.div(new BigDecimal(1.5), 10000, 5);
        BigDecimal mul = NumberUtil.mul(price, new BigDecimal(1.5));
        System.out.println(price);
    }
}
